/**
 * 
 */
package com.googlecode.agiletools.dailystandup;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Administrator
 * 
 */
public class DailyStandupManager {

    public static void main(String[] a) {
	System.out.println(DateUtils.round(Calendar.getInstance().getTime(),
		Calendar.DATE));
	System.out.println(Calendar.getInstance().getTime().getDay());
    }

    private HibernateTemplate hibernateTemplate;

    /**
     * @param id
     */
    @Transactional(readOnly = false)
    public void delete(int id) {
	hibernateTemplate.delete(get(id));

    }

    /**
     * @param dateFrom
     * @param dateTo
     */
    @SuppressWarnings("unchecked")
    public List<DailyStandup> find(Date from, Date to) {
	if (to == null) {
	    to = from;
	}
	DateUtils.round(from, Calendar.DATE);
	DateUtils.round(to, Calendar.DATE);
	to = DateUtils.addDays(to, 1);
	System.out.println(from);
	System.out.println(to);
	return hibernateTemplate
		.findByNamedParam(
			"from DailyStandup s where s.date >= :from and s.date < :to order by s.date",
			new String[] { "from", "to" },
			new Object[] { from, to });
    }

    /**
     * @param id
     * @return
     */
    public DailyStandup get(int id) {
	return (DailyStandup) hibernateTemplate.get(DailyStandup.class, id);
    }

    /**
     * @param dailyStandup
     * @return
     */
    @Transactional(readOnly = false)
    public DailyStandup save(DailyStandup dailyStandup) {
	if (dailyStandup == null) {
	    throw new IllegalArgumentException(
		    "Parameter dailyStandup can not be null.");
	}
	if (dailyStandup.getDate() == null) {
	    dailyStandup.setDate(Calendar.getInstance().getTime());
	}
	System.out.println("ID is " + dailyStandup.getId());
	if (dailyStandup.getId() > 0) {
	    hibernateTemplate.merge(dailyStandup);
	} else {
	    hibernateTemplate.persist(dailyStandup);
	}
	return dailyStandup;
    }

    /**
     * @param hibernateTemplate
     *            the hibernateTemplate to set
     */
    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
	this.hibernateTemplate = hibernateTemplate;
    }

}
